package cn.itguy.mobileguard;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

public class IMGPreferences {

	public static final String PREFERENCES_KEY_APP_FIRST_RUN = "app_first_run";
	public static final String PREFERENCES_KEY_NETWORK_STATE = "network_state";
	public static final String PREFERENCES_KEY_ALLCLEAR_GESTURE_ID = "allclear_gesture_id";
	public static final String PREFERENCES_KEY_ALLCLEAR_METHOD_INDEX = "allclear_method_index";
	public static final String PREFERENCES_KEY_PASSWORD = "password";
	public static final String PREFERENCES_KEY_ALARM_RING_INDEX = "alarm_ring_index";
	public static final String PREFERENCES_KEY_SENSOR_SENSITIVITY = "sensor_sensitivity";
	public static final String PREFERENCES_KEY_COUNT_DIALOG_TIME = "count_dialog_time";
	public static final String PREFERENCES_KEY_CURRENT_VERSION_CODE = "current_version_code";
	public static final String PREFERENCES_KEY_LAST_CONSUME_TIME = "last_consume_time";
	public static final String PREFERENCES_KEY_OFFERS_ENTRANCE_SWITCH = "offers_entrance_switch";
	
	private static IMGPreferences instance;
	
	private SharedPreferences sp;
	
	private IMGPreferences(Context context) {
		this.sp = PreferenceManager.getDefaultSharedPreferences(context);
	}
	
	public SharedPreferences getSp() {
		return sp;
	}

	public static final IMGPreferences getInstance(Context context) {
		if (instance == null) {
			instance = new IMGPreferences(context);
		}
		return instance;
	}

	public void setNetworkStateFlag(boolean available) {
		sp.edit().putBoolean(PREFERENCES_KEY_NETWORK_STATE, available).commit();
	}

	public boolean getNetworkStateFlag() {
		return sp.getBoolean(PREFERENCES_KEY_NETWORK_STATE, false);
	}

	public void setAllclearGestureId(long id) {
		sp.edit().putLong(PREFERENCES_KEY_ALLCLEAR_GESTURE_ID, id).commit();
	}
	
	public long getAllclearGestureId() {
		return sp.getLong(PREFERENCES_KEY_ALLCLEAR_GESTURE_ID, -1l);
	}

	public void setAllclearMethodIndex(int index) {
		sp.edit().putInt(PREFERENCES_KEY_ALLCLEAR_METHOD_INDEX, index).commit();
	}
	
	public int getAllclearMethodIndex() {
		return sp.getInt(PREFERENCES_KEY_ALLCLEAR_METHOD_INDEX, 0);
	}

	public void setPassword(String psw) {
		sp.edit().putString(PREFERENCES_KEY_PASSWORD, psw).commit();
	}
	
	public String getPassword() {
		return sp.getString(PREFERENCES_KEY_PASSWORD, "");
	}

	public int getAlarmRingIndex() {
		return sp.getInt(PREFERENCES_KEY_ALARM_RING_INDEX, 0);
	}
	
	public void setAlarmRingIndex(int index) {
		sp.edit().putInt(PREFERENCES_KEY_ALARM_RING_INDEX, index).commit();
	}

	public void setSensorSensitivity(int sensitivity) {
		sp.edit().putInt(PREFERENCES_KEY_SENSOR_SENSITIVITY, sensitivity).commit();
	}
	
	public int getSensorSensitivity() {
		return sp.getInt(PREFERENCES_KEY_SENSOR_SENSITIVITY, 2);
	}
	
	public void setCountDialogTime(int time) {
		sp.edit().putInt(PREFERENCES_KEY_COUNT_DIALOG_TIME, time).commit();
	}

	public int getCountDialogTime() {
		return sp.getInt(PREFERENCES_KEY_COUNT_DIALOG_TIME, 3);
	}

	public int getCurrentVersionCode() {
		return sp.getInt(PREFERENCES_KEY_CURRENT_VERSION_CODE, 0);
	}

	public void setCurrentVersionCode(int vCode) {
		sp.edit().putInt(PREFERENCES_KEY_CURRENT_VERSION_CODE, vCode).commit();
	}
	
	public void setLastConsumeTime(long time) {
		sp.edit().putLong(PREFERENCES_KEY_LAST_CONSUME_TIME, time).commit();
	}

	public long getLastConsumeTime() {
		return sp.getLong(PREFERENCES_KEY_LAST_CONSUME_TIME, 0);
	}

	public void setOffersEntranceSwitch(boolean on) {
		sp.edit().putBoolean(PREFERENCES_KEY_OFFERS_ENTRANCE_SWITCH, on).commit();
	}
	
	public boolean getOffersEntranceSwitch() {
		return sp.getBoolean(PREFERENCES_KEY_OFFERS_ENTRANCE_SWITCH, true);
	}

}
